<template>
  <div class="visitor-container">
    <div class="flex-row justify-between">
      <!-- search -->
      <div class="searchBox">
        <el-form inline :model="queryInfo" class="demo-form-inline" label-position="right" label-width="80px">
          <el-form-item label="名称">
            <el-input v-model="queryInfo.name" style="width: 200px;" placeholder="请输入类型名称" />
          </el-form-item>
        </el-form>
      </div>
      <div class="mg-top-4" style="flex-shrink: 0;">
        <el-button type="info" size="mini" @click="handleQuery">查询</el-button>
        <el-button size="mini" @click="reset">重置</el-button>
      </div>
    </div>
    <add-button title="新增" @addClick="handleAdd" size="mini" />
    <el-table
      v-loading="listLoading"
      :data="tableData" border
      header-cell-class-name="table-header-cell"
      header-row-class-name="table-header-row"
      class="table-container"
    >
      <el-table-column  prop="imgId" align="center"  label="类型图标">
        <!-- <template slot-scope="scope">
          <img v-if="scope.row.imgId" :src="scope.row.workshopImgId | imageFormat" class="list_avatar"/>
        </template> -->
      </el-table-column>
      <el-table-column prop="name" label="类型名称" />
      <el-table-column  prop="algorithmId" align="center"  label="匹配算法">
        <template slot-scope="scope">
          {{ scope.row.algorithmId }}
        </template>
      </el-table-column>
      <el-table-column prop="node" label="备注" />
      <el-table-column label="操作" align="center"  width="200" >
        <template slot-scope="scope">
           <el-link type="primary" :underline="false" class="upStr fz-1" @click="updateDetails(scope.row.id)" >
            修改
          </el-link>
          <span class="line">|</span>
          <el-link type="danger" :underline="false" class="upStr redBtn"  @click="submitDelete(scope.row.id)">
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      :total="total"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="paging.pageSize"
      :current-page="currentPage"
      layout="total, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <!-- 添加或修改 -->
    <el-dialog :title="dialogtit" :visible.sync="dialogFormVisibleAdd" width="40%">
      <el-form ref="pushData" :model="pushData" label-position="right" :rules="rules" class="addForm" label-width="80px" v-loading="formDataLoad">
        <el-form-item label="类型名称"  prop="name">
          <el-input v-model="pushData.name" style="width: 250px;"></el-input>
        </el-form-item>
        <el-form-item label="图标上传"  prop="imgId">
          <el-input v-model="pushData.imgId" style="width: 250px;"></el-input>
        </el-form-item>
        <el-form-item label="图标上传" ref="imgId"  >
          <UploadImg is-show-tip :limit="1" @success="handleUploadFileSuccess">
            <div class="upload-info">
              <img v-if="imageUrl" :src="imageUrl | imageFormat" class="avatar"/>
              <i v-else class="el-icon-plus avatar-uploader-icon" style="width: 100px; height: 100px;" />
            </div>
          </UploadImg>
        </el-form-item>
        <el-form-item label="可用算法"  prop="algorithmId">
          <el-input v-model="pushData.algorithmId" style="width: 250px;"></el-input>
        </el-form-item>
        <el-form-item label="备注说名"  prop="note">
          <el-input v-model="pushData.note" style="width: 250px;"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit('pushData')">提交</el-button>
        <el-button @click="cancal">取消</el-button>
      </div>
    </el-dialog>
    <!-- 删除车间 -->
  </div>
</template>

<script>
import { findEquipmentTypePageByParam, delAlgorithmById, addAlgorithm, updateAlgorithm, findAlgorithmById } from '@/child-business/api/IndustrialVision' 
import { handleImage } from '@/utils/tools'
import AddButton from '@/components/add-button/index.vue'
export default {
  name: 'Algorithm',
  components: { AddButton },
  data() {
    return {
      imageUrl: '',
      typeList: [
        {typeName: '全部', id: 0 },
        {typeName: '园区', id: 1 },
        {typeName: '企业', id: 2 },
        {typeName: '车间', id: 3 },
      ],
      dialogtit: '新增',
      listLoading: true,
      currentPage: 1, // 默认当前页
      dialogFormVisibleAdd: false, // 新增弹窗
      formDataLoad: false,
      dialogVisible: false, // 提交
      paging: {
        pageNo: 1,
        pageSize: 12
      },
      queryInfo: {
        name: ''
      },
      total: 0,
      tableData: [],
      pushData: {
        id: null,
        name: '', // 名称
        algorithmId: '', // 算法来源
        node: '' // 备注说明
      },
      rules: {
        name: [{ required: true, message: '请输入算法名称', trigger: 'blur' }]
      },
      equimentTypeList: []
    }
  },
  filters: {
    imageFormat(value) { // 返回图片地址
      return handleImage(value)
    },
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    // 上传图片
    async handleUploadFileSuccess(fileList) {
      this.pushData.workshopImgId = fileList[0].fileId
      this.imageUrl = this.pushData.workshopImgId
      // 预览地址 url
    },
    // 查询
    handleQuery() {
      this.paging.pageNo = 1
      this.getList()
    },
    // 查询数据
    getList() {
      this.listLoading = true
      findEquipmentTypePageByParam({ equipmentTypeVO: this.queryInfo, ...this.paging}).then(response => {
        this.total = response.total
        this.tableData = response.records
        this.paging.pageNo = response.pageNo
        this.paging.pageSize = response.pageSize
        this.listLoading = false
      })
    },
    //重置
    reset() {
      this.paging.pageNo = 1
      this.queryInfo = { name: '' }
      this.getList()
    },
    // 添加产品信息按钮
    handleAdd() {
      this.pushData = {
        id: null,
      }
      this.dialogFormVisibleAdd = true
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.paging.pageNo = val
      this.getList()
    },
    // 取消提交添加产品信息
    cancal() {
      this.dialogFormVisibleAdd = false
    },
    // 提交添加产品信息
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.pushData.id != null) {
            updateAlgorithm(this.pushData).then(response => {
              if (response) {
                this.dialogFormVisibleAdd = false
                this.$message({ message: '修改成功！', type: 'success' })                
              }else{
                this.$message({ message: '修改失败！', type: 'error' })       
              }
              this.getList()
            })
          } else {
            delete this.pushData.id
            addAlgorithm(this.pushData).then(response => {
              if (response) {
                this.dialogFormVisibleAdd = false
                this.$message({ message: '消息添加成功！', type: 'success' })
                this.getList()
              }
            })
          }
        } else {
          return false
        }
      })
    },
    // 修改产品信息
    updateDetails(id) {
      this.dialogtit = '修改'
      this.dialogFormVisibleAdd = true
      this.formDataLoad = true
      findAlgorithmById({ AlgorithmId: id}).then(response => {
        this.pushData = response
        this.pushData.id = response.id
        this.formDataLoad = false
      })
    },
    // 删除提交
    submitDelete(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
      }).then(() => {
        delAlgorithmById({ AlgorithmId: id }).then(response => {
          if (response) {
            this.$message({ message: '删除成功！', type: 'success' })
          } else {
            this.$message({ message: '删除失败！', type: 'error' })
          }
          this.getList()
        })
      }).catch(() => { })
    },
  }
}
</script>
<style lang="scss" scoped>
 @import '@/child-business/styles/IndustrialVision.scss';
::v-deep .el-form-item__label{text-align: center;}
</style>
